/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package server.xmpp.pingmodule;

import org.apache.vysper.xmpp.modules.core.base.handler.DefaultIQHandler;
import org.apache.vysper.xmpp.protocol.NamespaceURIs;
import org.apache.vysper.xmpp.server.ServerRuntimeContext;
import org.apache.vysper.xmpp.server.SessionContext;
import org.apache.vysper.xmpp.stanza.IQStanza;
import org.apache.vysper.xmpp.stanza.IQStanzaType;
import org.apache.vysper.xmpp.stanza.Stanza;
import org.apache.vysper.xmpp.stanza.StanzaBuilder;
import server.UserManagement;
import server.xmpp.XMPPServerClass;

/**
 * @author User
 * 
 */
public class MyPingIQHandler extends DefaultIQHandler {

	private UserManagement um;
	XMPPServerClass server;
	
	public MyPingIQHandler() {
		um = UserManagement.getInstance();
		server = XMPPServerClass.getInstance(); 
	}

	/**
	 * Called for every received stanza, to verify, if this handler is
	 * interested in it
	 */
	@Override
	public boolean verify(Stanza stanza) {
		if (stanza == null)
			return false;

		boolean extension = super.verify(stanza);
		if (extension) {
			// System.out.println("verify / extension");
			return true;
		}
		return false;
	}

	/**
	 * Called if we want to look at stanza closer
	 */
	@Override
	protected boolean verifyInnerElement(Stanza stanza) {
		// System.out.println("verifyInnerElement");
		// does inner namespace correspond to what we look for?
		return verifyInnerElementWorker(stanza, "ping")
				&& verifyInnerNamespace(stanza, NamespaceURIs.URN_XMPP_PING);
	}

	/**
	 * If handler is responsible for stanza, handles it, returns response stanza
	 */
	@Override
	protected Stanza handleGet(IQStanza stanza,	ServerRuntimeContext serverRuntimeContext,
			SessionContext sessionContext) {
		
		System.out.println(String.format("Ping from %1$s", stanza.getFrom()
				.getFullQualifiedName()));

		um.addUser(stanza.getFrom());
		server.sendSomething(stanza.getFrom());
		
		StanzaBuilder stanzaBuilder = StanzaBuilder.createIQStanza(
				stanza.getTo(), stanza.getFrom(), IQStanzaType.RESULT,	stanza.getID());

		return stanzaBuilder.build();
	}

	@Override
	protected Stanza handleResult(IQStanza stanza,
			ServerRuntimeContext serverRuntimeContext,
			SessionContext sessionContext) {

		return null;
	}

	/**
	 * Called for every stanza
	 * 
	 */
	@Override
	protected boolean verifyNamespace(Stanza arg0) {
		// System.out.println("Verify namespace " +arg0.getNamespaceURI());
		// usually namespace is jabber:client

		return true;
	}

	/**
	 * Interrupts ping checking thread. Used on server shutdown
	 */
}
